import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ability/framework/state/base_state.dart';
import 'package:flutter_ability/framework/vm/base_refresh_view_model.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/// 封装通用的刷新分页框架
abstract class BaseRefreshState<M extends BaseRefreshViewModel,
        T extends ConsumerStatefulWidget> extends BaseState<M, T>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  ///真实分页控件
  Widget getContentChild(M model);

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SmartRefresher(
      controller: viewModel.refreshController,
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: viewModel.onRefresh,
      onLoading: viewModel.onLoading,
      header: const WaterDropHeader(),
      footer: buildCustomFooter(),
      child: getContentChild(viewModel),
    );
  }

  CustomFooter buildCustomFooter() {
    return CustomFooter(
      builder: (context, mode) {
        Widget body;
        print("mode = $mode");
        if (mode == LoadStatus.idle) {
          body = const Text("上拉加载",
              style: TextStyle(fontSize: 14, color: Colors.black45));
        } else if (mode == LoadStatus.loading) {
          body = const CupertinoActivityIndicator();
        } else if (mode == LoadStatus.failed) {
          body = const Text("加载失败!",
              style: TextStyle(fontSize: 14, color: Colors.black45));
        } else if (mode == LoadStatus.canLoading) {
          body = const Text("加载更多!",
              style: TextStyle(fontSize: 14, color: Colors.black45));
        } else {
          body = const SizedBox();
        }
        return SizedBox(
          height: 55.0,
          child: Center(child: body),
        );
      },
    );
  }
}
